/*
 * @(#)UpdateAlert.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.alert.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  UpdateAlertDAObject.java

  This class is the data access bean for table "UPDATE_ALERT".

  @author      Zoe Shum
  @company     DCIVision Limited
  @creation date   12/02/2004
  @version     $Revision: 1.33.4.1 $
*/
/* ********************************Modify History***********************************************
 * Modify by  : Jicle.Wang 
 * Modify date: 2006/08/31
 * BugID      : 6051
 * Description: 'Due Date' and 'Message' are lost when document be invitated more than noce.
 * *********************************************************************************************
 */
public class UpdateAlertDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.33.4.1 $";

  public static final String TABLE_NAME = "UPDATE_ALERT";

  public UpdateAlertDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("OBJECT_TYPE");
    this.vecDBColumn.add("OBJECT_ID");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  
  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ,A.NOTIFY_SELF ");
        sqlStat.append("FROM   UPDATE_ALERT A ");
        sqlStat.append("WHERE  A.ID = ? ");
//        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
//        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          UpdateAlert tmpUpdateAlert = new UpdateAlert();
          tmpUpdateAlert.setID(getResultSetInteger(rs, "ID"));
          tmpUpdateAlert.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUpdateAlert.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUpdateAlert.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUpdateAlert.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUpdateAlert.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUpdateAlert.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUpdateAlert.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUpdateAlert.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUpdateAlert.setCreatorName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getCreatorID()));
          tmpUpdateAlert.setUpdaterName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getUpdaterID()));
          tmpUpdateAlert.setNotifySelf(getResultSetString(rs,"NOTIFY_SELF"));
          return(tmpUpdateAlert);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.NOTIFY_SELF ");
        sqlStat.append("FROM   UPDATE_ALERT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        /*
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }
        */
        
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          UpdateAlert tmpUpdateAlert = new UpdateAlert();
          tmpUpdateAlert.setID(getResultSetInteger(rs, "ID"));
          tmpUpdateAlert.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUpdateAlert.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUpdateAlert.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUpdateAlert.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUpdateAlert.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUpdateAlert.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUpdateAlert.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUpdateAlert.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUpdateAlert.setCreatorName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getCreatorID()));
          tmpUpdateAlert.setUpdaterName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getUpdaterID()));
          tmpUpdateAlert.setRecordCount(totalNumOfRecord);
          tmpUpdateAlert.setRowNum(startOffset++);
          tmpUpdateAlert.setNotifySelf(getResultSetString(rs,"NOTIFY_SELF"));
          ++rowLoopCnt;
          result.add(tmpUpdateAlert);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  
  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.NOTIFY_SELF ");
        sqlStat.append("FROM   UPDATE_ALERT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UpdateAlert tmpUpdateAlert = new UpdateAlert();
          tmpUpdateAlert.setID(getResultSetInteger(rs, "ID"));
          tmpUpdateAlert.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUpdateAlert.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUpdateAlert.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUpdateAlert.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUpdateAlert.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUpdateAlert.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUpdateAlert.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUpdateAlert.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUpdateAlert.setCreatorName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getCreatorID()));
          tmpUpdateAlert.setUpdaterName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getUpdaterID()));
          tmpUpdateAlert.setNotifySelf(getResultSetString(rs,"NOTIFY_SELF"));
          result.add(tmpUpdateAlert);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    UpdateAlert tmpUpdateAlert = (UpdateAlert)((UpdateAlert)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   UPDATE_ALERT(ID, OBJECT_TYPE, OBJECT_ID, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE, NOTIFY_SELF) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpUpdateAlert.getObjectType());
        setPrepareStatement(preStat, 3, tmpUpdateAlert.getObjectID());
        setPrepareStatement(preStat, 4, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 5, new Integer(0));
        setPrepareStatement(preStat, 6, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 7, currTime);
        setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 9, currTime);
        setPrepareStatement(preStat,10,tmpUpdateAlert.getNotifySelf());
        preStat.executeUpdate();
        tmpUpdateAlert.setID(nextID);
        tmpUpdateAlert.setCreatorID(sessionContainer.getUserRecordID());
        tmpUpdateAlert.setCreateDate(currTime);
        tmpUpdateAlert.setUpdaterID(sessionContainer.getUserRecordID());
        tmpUpdateAlert.setUpdateDate(currTime);
        tmpUpdateAlert.setUpdateCount(new Integer(0));
        tmpUpdateAlert.setCreatorName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getCreatorID()));
        tmpUpdateAlert.setUpdaterName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getUpdaterID()));
        return(tmpUpdateAlert);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    UpdateAlert tmpUpdateAlert = (UpdateAlert)((UpdateAlert)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE UPDATE_ALERT ");
        sqlStat.append("SET  OBJECT_TYPE=?, OBJECT_ID=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=?, NOTIFY_SELF=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpUpdateAlert.getObjectType());
        setPrepareStatement(preStat, 2, tmpUpdateAlert.getObjectID());
        setPrepareStatement(preStat, 3, new Integer(tmpUpdateAlert.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 4, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 5, currTime);
        setPrepareStatement(preStat,6,tmpUpdateAlert.getNotifySelf());
        setPrepareStatement(preStat, 7, tmpUpdateAlert.getID());
        setPrepareStatement(preStat, 8, tmpUpdateAlert.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpUpdateAlert.setUpdaterID(sessionContainer.getUserRecordID());
          tmpUpdateAlert.setUpdateDate(currTime);
          tmpUpdateAlert.setUpdateCount(new Integer(tmpUpdateAlert.getUpdateCount().intValue() + 1));
          tmpUpdateAlert.setCreatorName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getCreatorID()));
          tmpUpdateAlert.setUpdaterName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getUpdaterID()));
          return(tmpUpdateAlert);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    UpdateAlert tmpUpdateAlert = (UpdateAlert)((UpdateAlert)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   UPDATE_ALERT ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpUpdateAlert.getID());
        setPrepareStatement(preStat, 2, tmpUpdateAlert.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpUpdateAlert);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    UpdateAlert tmpUpdateAlert = (UpdateAlert)this.oldValue;

    if (tmpUpdateAlert != null) {
      oldValues.add(toAuditTrailValue(tmpUpdateAlert.getID()));
      oldValues.add(toAuditTrailValue(tmpUpdateAlert.getObjectType()));
      oldValues.add(toAuditTrailValue(tmpUpdateAlert.getObjectID()));
      oldValues.add(toAuditTrailValue(tmpUpdateAlert.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpUpdateAlert.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpUpdateAlert.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpUpdateAlert.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpUpdateAlert.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpUpdateAlert.getUpdateDate()));
    }

    tmpUpdateAlert = (UpdateAlert)obj;
    if (tmpUpdateAlert != null) {
      newValues.add(toAuditTrailValue(tmpUpdateAlert.getID()));
      newValues.add(toAuditTrailValue(tmpUpdateAlert.getObjectType()));
      newValues.add(toAuditTrailValue(tmpUpdateAlert.getObjectID()));
      newValues.add(toAuditTrailValue(tmpUpdateAlert.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpUpdateAlert.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpUpdateAlert.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpUpdateAlert.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpUpdateAlert.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpUpdateAlert.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  public synchronized AbstractBaseObject getByObjectTypeObjectID( String objectType, Integer objectID) throws ApplicationException {
    return getByObjectTypeObjectIDRecordStatus(objectType,objectID,GlobalConstant.STATUS_ACTIVE);
  }

  public synchronized AbstractBaseObject getByObjectTypeObjectIDRecordStatus( String objectType, Integer objectID,String recordStatus) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    //List resultList = new ArrayList();
    UpdateAlert tmpUpdateAlert = new UpdateAlert();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.NOTIFY_SELF ");
        sqlStat.append("FROM   UPDATE_ALERT A WHERE ( A.OBJECT_TYPE = ? ) ");
        sqlStat.append(" AND ( A.OBJECT_ID = ? ) ");
        if(recordStatus!=null){
          sqlStat.append(" AND (A.RECORD_STATUS = ? ) ");
        }

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, objectType);
        this.setPrepareStatement(preStat, 2, objectID);
        if(recordStatus!=null){
          this.setPrepareStatement(preStat, 3, recordStatus);
        }

        rs = preStat.executeQuery();
        if (rs.next()) {
          tmpUpdateAlert.setID(getResultSetInteger(rs, "ID"));
          tmpUpdateAlert.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUpdateAlert.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUpdateAlert.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUpdateAlert.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUpdateAlert.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUpdateAlert.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUpdateAlert.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUpdateAlert.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUpdateAlert.setNotifySelf(getResultSetString(rs,"NOTIFY_SELF"));
          return tmpUpdateAlert;
        }else{
          return null;
        }

      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }

  }
  
  public synchronized AbstractBaseObject getInactiveByObjectTypeObjectID( String objectType, Integer objectID) throws ApplicationException {
    return getByObjectTypeObjectIDRecordStatus(objectType,objectID,GlobalConstant.STATUS_INACTIVE);
  }

  public synchronized List getByObjectTypeObjectIDList(String objectType, Integer objectID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List resultList = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.NOTIFY_SELF ");
        sqlStat.append("FROM   UPDATE_ALERT A ");
        sqlStat.append("WHERE  A.OBJECT_TYPE = ? AND A.OBJECT_ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, objectType);
        this.setPrepareStatement(preStat, 2, objectID);
        this.setPrepareStatement(preStat, 3, GlobalConstant.STATUS_ACTIVE);

        rs = preStat.executeQuery();
        while (rs.next()) {
          UpdateAlert tmpUpdateAlert = new UpdateAlert();
          tmpUpdateAlert.setID(getResultSetInteger(rs, "ID"));
          tmpUpdateAlert.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUpdateAlert.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUpdateAlert.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUpdateAlert.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUpdateAlert.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUpdateAlert.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUpdateAlert.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUpdateAlert.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUpdateAlert.setNotifySelf(getResultSetString(rs,"NOTIFY_SELF"));
          resultList.add(tmpUpdateAlert);
        }
      } catch (ApplicationException appEx) {
          throw appEx;
      } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return resultList;
  }

  public synchronized List getByObjectTypeObjectIDAndActionType(String objectType, Integer objectID, String actionType) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List resultList = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, UT.ALERT_TIME, UT.ALERT_TIME_TYPE, UT.DUE_DATE, UT.MESSAGE, UT.STATUS, UT.RECUR_EVENT, A.UPDATE_COUNT, A.RECORD_STATUS, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.NOTIFY_SELF ");
        sqlStat.append("FROM   UPDATE_ALERT A, UPDATE_ALERT_TYPE UT ");
        sqlStat.append("WHERE  A.ID = UT.UPDATE_ALERT_ID AND A.OBJECT_TYPE = ? AND A.OBJECT_ID = ? AND UT.ACTION_TYPE = ? AND UT.RECORD_STATUS = ? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, objectType);
        this.setPrepareStatement(preStat, 2, objectID);
        this.setPrepareStatement(preStat, 3, actionType);
        this.setPrepareStatement(preStat, 4, GlobalConstant.STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UpdateAlert tmpUpdateAlert = new UpdateAlert();
          tmpUpdateAlert.setID(getResultSetInteger(rs, "ID"));
          tmpUpdateAlert.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUpdateAlert.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUpdateAlert.setAlertTime(getResultSetInteger(rs, "ALERT_TIME"));
          tmpUpdateAlert.setAlertTimeType(getResultSetString(rs, "ALERT_TIME_TYPE"));
          tmpUpdateAlert.setDueDate(getResultSetTimestamp(rs, "DUE_DATE"));
          tmpUpdateAlert.setMessage(getResultSetString(rs, "MESSAGE"));
          tmpUpdateAlert.setStatus(getResultSetString(rs, "STATUS"));
          tmpUpdateAlert.setRecurEvent(getResultSetString(rs, "RECUR_EVENT"));
          tmpUpdateAlert.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUpdateAlert.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUpdateAlert.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUpdateAlert.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUpdateAlert.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUpdateAlert.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUpdateAlert.setNotifySelf(getResultSetString(rs,"NOTIFY_SELF"));
          resultList.add(tmpUpdateAlert);
        }
      } catch (ApplicationException appEx) {
          throw appEx;
      } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return resultList;
  }

  public synchronized List getByActionType(String actionType) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List resultList = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, UT.ALERT_TIME, UT.ALERT_TIME_TYPE, UT.DUE_DATE, UT.MESSAGE, UT.STATUS, UT.RECUR_EVENT, A.RECORD_STATUS, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.NOTIFY_SELF ");
        sqlStat.append("FROM   UPDATE_ALERT A, UPDATE_ALERT_TYPE UT ");
        sqlStat.append("WHERE  A.ID = UT.UPDATE_ALERT_ID AND UT.ACTION_TYPE = ? AND UT.RECORD_STATUS = ?");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, actionType);
        this.setPrepareStatement(preStat, 2, GlobalConstant.STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UpdateAlert tmpUpdateAlert = new UpdateAlert();
          tmpUpdateAlert.setID(getResultSetInteger(rs, "ID"));
          tmpUpdateAlert.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUpdateAlert.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUpdateAlert.setAlertTime(getResultSetInteger(rs, "ALERT_TIME"));
          tmpUpdateAlert.setAlertTimeType(getResultSetString(rs, "ALERT_TIME_TYPE"));
          tmpUpdateAlert.setDueDate(getResultSetTimestamp(rs, "DUE_DATE"));
          tmpUpdateAlert.setMessage(getResultSetString(rs, "MESSAGE"));
          tmpUpdateAlert.setStatus(getResultSetString(rs, "STATUS"));
          tmpUpdateAlert.setRecurEvent(getResultSetString(rs, "RECUR_EVENT"));
          tmpUpdateAlert.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUpdateAlert.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUpdateAlert.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUpdateAlert.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUpdateAlert.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUpdateAlert.setUpdateCount(getResultSetInteger(rs,"NOTIFY_SELF"));

          resultList.add(tmpUpdateAlert);
        }
      } catch (ApplicationException appEx) {
          throw appEx;
      } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return resultList;
  }

  public synchronized AbstractBaseObject getByObjectTypeObjectIDCreatorID(String objectType, Integer objectID, Integer creatorID) throws ApplicationException {
    return getByObjectTypeObjectIDCreatorID(objectType, objectID, creatorID, null);
  }
  public synchronized AbstractBaseObject getByObjectTypeObjectIDCreatorID(String objectType, Integer objectID, Integer creatorID, String alertType) throws ApplicationException {

    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    UpdateAlert tmpUpdateAlert = new UpdateAlert();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, T.ALERT_TIME, T.ALERT_TIME_TYPE, T.DUE_DATE, T.MESSAGE, T.STATUS, T.RECUR_EVENT, T.ALERT_SUBJECT, A.UPDATE_COUNT, A.RECORD_STATUS, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.NOTIFY_SELF ");
        sqlStat.append("FROM   UPDATE_ALERT A, UPDATE_ALERT_TYPE T ");
        sqlStat.append("WHERE  T.RECORD_STATUS = ? AND T.UPDATE_ALERT_ID = A.ID AND A.OBJECT_TYPE=? AND A.OBJECT_ID=? AND T.CREATOR_ID=? ");
        if (alertType!=null) {
          sqlStat.append("AND  (T.ACTION_TYPE = ? OR T.ACTION_TYPE = ? ) ");
        } else {
          sqlStat.append("AND  T.ACTION_TYPE <> 'I' ");
        }
        //log.debug(sqlStat.toString()) ;
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, objectType);
        this.setPrepareStatement(preStat, 3, objectID);
        this.setPrepareStatement(preStat, 4, creatorID);
        if (alertType!=null){
          this.setPrepareStatement(preStat, 5, alertType);
          this.setPrepareStatement(preStat, 6, UpdateAlert.DISCUSS_ACTION);
        }  
        rs = preStat.executeQuery();
        if (rs.next()) {
          tmpUpdateAlert.setID(getResultSetInteger(rs, "ID"));
          tmpUpdateAlert.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUpdateAlert.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
 //         tmpUpdateAlert.setup
          tmpUpdateAlert.setAlertTime(getResultSetInteger(rs, "ALERT_TIME"));
          tmpUpdateAlert.setAlertTimeType(getResultSetString(rs, "ALERT_TIME_TYPE"));
          tmpUpdateAlert.setAlertSubject(getResultSetString(rs, "ALERT_SUBJECT"));
          tmpUpdateAlert.setDueDate(getResultSetTimestamp(rs, "DUE_DATE"));
          tmpUpdateAlert.setMessage(getResultSetString(rs, "MESSAGE"));
          tmpUpdateAlert.setStatus(getResultSetString(rs, "STATUS"));
          tmpUpdateAlert.setRecurEvent(getResultSetString(rs, "RECUR_EVENT"));
          tmpUpdateAlert.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUpdateAlert.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUpdateAlert.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUpdateAlert.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUpdateAlert.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUpdateAlert.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUpdateAlert.setNotifySelf(getResultSetString(rs,"NOTIFY_SELF"));

          return tmpUpdateAlert;
        } else {
          return null;
        }

      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  
  public synchronized void hardDelete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
	    StringBuffer sqlStat = new StringBuffer();
	    UpdateAlert tmpUpdateAlert = (UpdateAlert)((UpdateAlert)obj).clone();

	    synchronized(dbConn) {
	      try {
	        int updateCnt = 0;
		        sqlStat.append("DELETE ");
		        sqlStat.append("FROM   UPDATE_ALERT ");
		        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
		        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		        setPrepareStatement(preStat, 1, tmpUpdateAlert.getID());
		        setPrepareStatement(preStat, 2, tmpUpdateAlert.getUpdateCount());
		        updateCnt = preStat.executeUpdate();
					
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
	      } finally {
	        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	      }
	    }
  }
  
  public synchronized AbstractBaseObject updateStatus(AbstractBaseObject obj) throws ApplicationException {
	    PreparedStatement preStat = null;
	    StringBuffer sqlStat = new StringBuffer();
	    UpdateAlert tmpUpdateAlert = (UpdateAlert)((UpdateAlert)obj).clone();

	    synchronized(dbConn) {
	      try {
	        int updateCnt = 0;
	        Timestamp currTime = Utility.getCurrentTimestamp();
	        sqlStat.append("UPDATE UPDATE_ALERT ");
	        sqlStat.append("SET  record_status=?");
	        sqlStat.append("WHERE  object_type=? and object_id=?");
	        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        setPrepareStatement(preStat, 1,GlobalConstant.RECORD_STATUS_INACTIVE );
	        setPrepareStatement(preStat, 2, tmpUpdateAlert.getObjectType());
	        setPrepareStatement(preStat, 3, tmpUpdateAlert.getObjectID());	       
	        updateCnt = preStat.executeUpdate();
	        if (updateCnt == 0) {
	          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
	        } else {
	          tmpUpdateAlert.setUpdaterID(sessionContainer.getUserRecordID());
	          tmpUpdateAlert.setUpdateDate(currTime);
	          if(tmpUpdateAlert.getUpdateCount()==null){
	        	  tmpUpdateAlert.setUpdateCount(new Integer(0));
	          }
	          tmpUpdateAlert.setUpdateCount(new Integer(tmpUpdateAlert.getUpdateCount().intValue() + 1));
	          tmpUpdateAlert.setCreatorName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getCreatorID()));
	          tmpUpdateAlert.setUpdaterName(UserInfoFactory.getUserFullName(tmpUpdateAlert.getUpdaterID()));
	          return(tmpUpdateAlert);
	        }
	      } catch (ApplicationException appEx) {
	        throw appEx;
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
	      } finally {
	        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	      }
	    }
	  }
  	
  public synchronized void deleteByID(Integer ID) throws ApplicationException {
	    PreparedStatement preStat = null;
	    StringBuffer sqlStat = new StringBuffer();

	    synchronized(dbConn) {
	      try {
	        sqlStat.append("DELETE ");
	        sqlStat.append("FROM   UPDATE_ALERT ");
	        sqlStat.append("WHERE  ID=? ");
	        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        setPrepareStatement(preStat, 1, ID);
	        preStat.executeUpdate();
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
	      } finally {
	        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	      }
	    }
	  }
  
  public synchronized Integer getInvitationIDByAlertIDorSystemLogID(
      Integer updateAlertID,
      Integer updateAlertSystemLogID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    if (updateAlertID==null && updateAlertSystemLogID==null) {
      return null;
    }
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID ");
        sqlStat.append("FROM UPDATE_ALERT A, UPDATE_ALERT_TYPE B, UPDATE_ALERT_SYSTEM_LOG C ");
        sqlStat.append("WHERE A.ID=B.UPDATE_ALERT_ID AND B.ID=C.UPDATE_ALERT_TYPE_ID ");
        if (updateAlertID!=null) {
          sqlStat.append(" AND A.ID = ? ");
        }
        sqlStat.append(" AND B.ACTION_TYPE = 'I' ");
        if (updateAlertSystemLogID!=null) {
          sqlStat.append(" AND C.ID = ? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int counter = 1;
        if (updateAlertID!=null) {
          this.setPrepareStatement(preStat, counter++, updateAlertID);
        }
        if (updateAlertSystemLogID!=null) {
          this.setPrepareStatement(preStat, counter++, updateAlertSystemLogID);
        }
        rs = preStat.executeQuery();
        if (rs.next()) {
          return getResultSetInteger(rs, "ID");
        }else {
          return null;
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /* Jicle.Wang 6051 2006/08/31 Start */
  public synchronized AbstractBaseObject getByUserID( String objectType, Integer userId, Integer objectId) throws ApplicationException {
	    PreparedStatement preStat = null;
	    ResultSet rs = null;
	    StringBuffer sqlStat = new StringBuffer();
	    //List resultList = new ArrayList();
	    UpdateAlert tmpUpdateAlert = new UpdateAlert();

	    synchronized(dbConn) {
	      try {
	        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.NOTIFY_SELF ");
	        sqlStat.append("FROM   UPDATE_ALERT A WHERE ( A.OBJECT_TYPE = ? ) ");
	        sqlStat.append(" AND ( A.CREATOR_ID = ? ) ");
            sqlStat.append(" AND (A.RECORD_STATUS = ? ) ");
            sqlStat.append(" AND ( A.OBJECT_ID = ? ) ");
	        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        this.setPrepareStatement(preStat, 1, objectType);
	        this.setPrepareStatement(preStat, 2, userId);
	        this.setPrepareStatement(preStat, 3, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStat, 4, objectId);

	        rs = preStat.executeQuery();
	        if (rs.next()) {
	          tmpUpdateAlert.setID(getResultSetInteger(rs, "ID"));
	          tmpUpdateAlert.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
	          tmpUpdateAlert.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
	          tmpUpdateAlert.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
	          tmpUpdateAlert.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
	          tmpUpdateAlert.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
	          tmpUpdateAlert.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
	          tmpUpdateAlert.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
	          tmpUpdateAlert.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
	          tmpUpdateAlert.setNotifySelf(getResultSetString(rs,"NOTIFY_SELF"));
	          return tmpUpdateAlert;
	        }else{
	          return null;
	        }

	      } catch (ApplicationException appEx) {
	        throw appEx;
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
	      } finally {
	        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
	        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	      }
	    }

	  }
  /* Jicle.Wang 6051 2006/08/31 End   */
  
}
